package com.hankai.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.hankai.common.ResponseCode;
import com.hankai.common.ServerResponse;
import com.hankai.dao.CategoryMapper;
import com.hankai.dao.ProductMapper;
import com.hankai.pojo.Category;
import com.hankai.pojo.Product;
import com.hankai.service.IProductService;
import com.hankai.util.DateTimeUtil;
import com.hankai.util.PropertiesUtil;
import com.hankai.vo.ProductDetailVo;
import com.hankai.vo.ProductListVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by hankai on 2017/6/7.
 */
@Service
public class ProductServiceImpl implements IProductService {
    @Resource
    private ProductMapper productMapper;
    @Resource
    private CategoryMapper categoryMapper;
    @Override
    public ServerResponse saveOrUpdateProduct(Product product) {
        if(product!=null){
            if(StringUtils.isNotBlank(product.getSubImages())){
                String[] str = product.getSubImages().split(",");
                if(str.length>0){
                    String mainImages = str[0];
                }
            }

            if(product.getId()!=null){
                if(productMapper.updateByPrimaryKeySelective(product)>0){
                    return ServerResponse.createBySuccess("修改成功");
                }
                return ServerResponse.createByError("修改失败");
            }else{
                if(productMapper.insert(product)>0){
                    return ServerResponse.createBySuccess("添加成功");
                }
                return ServerResponse.createByError("添加失败");
            }
        }
        return ServerResponse.createByError("参数有误");
    }

    @Override
    public ServerResponse updateState(Integer productId, Integer status) {
        if(productId==null || status==null){
            return ServerResponse.createByError(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        Product product = new Product();
        product.setId(productId);
        product.setStatus(status);
        if(productMapper.updateByPrimaryKeySelective(product)>0){
            return ServerResponse.createBySuccess("修改成功");
        }
        return ServerResponse.createByError("修改失败");
    }

    @Override
    public ServerResponse manageProductDetail(Integer productId) {
        if(productId==null){
            return ServerResponse.createByError(ResponseCode.ILLEGAL_ARGUMENT.getCode(),ResponseCode.ILLEGAL_ARGUMENT.getDesc());
        }
        Product product = productMapper.selectByPrimaryKey(productId);
        if(product==null){
            return ServerResponse.createByError("产品已下架或已删除");
        }

        ProductDetailVo productDetailVo =assembleProductDatailVo(product);
        return ServerResponse.createBySuccess(productDetailVo);
    }

    private ProductDetailVo assembleProductDatailVo(Product product){
        ProductDetailVo productDetailVo = new ProductDetailVo();
        productDetailVo.setId(product.getId());
        productDetailVo.setSubtitle(product.getSubtitle());
        productDetailVo.setPrice(product.getPrice());
        productDetailVo.setMainImage(product.getMainImage());
        productDetailVo.setSubImages(product.getSubImages());
        productDetailVo.setCategoryId(product.getCategoryId());
        productDetailVo.setDetail(product.getDetail());
        productDetailVo.setName(product.getName());
        productDetailVo.setStatus(product.getStatus());
        productDetailVo.setStock(product.getStock());
        //配置文件
        productDetailVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix","http://image.hankai.com/"));
        //分类父id
        Category category = categoryMapper.selectByPrimaryKey(product.getCategoryId());
        if(category==null){
            productDetailVo.setCategoryId(0);
        }else{
            productDetailVo.setParentCategoryId(category.getParentId());
        }

        //创建时间
        productDetailVo.setCreateTime(DateTimeUtil.dateToStr(product.getCreateTime()));
        return productDetailVo;
    }

    @Override
    public ServerResponse<PageInfo> getProductList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Product> productList = productMapper.getProductList();
        List<ProductListVo>  productListVoList = Lists.newArrayList();
        for(Product product:productList){
            productListVoList.add(assembleProductListVo(product));
        }
        PageInfo pageInfo = new PageInfo(productList);
        pageInfo.setList(productListVoList);
        return ServerResponse.createBySuccess(pageInfo);
    }

    /**
     * 将product格式转换成productListvo格式
     * @param product
     * @return
     */
    private ProductListVo assembleProductListVo(Product product){
        ProductListVo productListVo = new ProductListVo();

        productListVo.setCategoryId(product.getCategoryId());
        productListVo.setImageHost(PropertiesUtil.getProperty("ftp.server.http.prefix","http://image.hankai.com/"));
        productListVo.setStatus(product.getStatus());
        productListVo.setId(product.getId());
        productListVo.setMainImage(product.getMainImage());
        productListVo.setName(product.getName());
        productListVo.setPrice(product.getPrice());
        productListVo.setSubtitle(product.getSubtitle());
        return productListVo;
    }

    @Override
    public ServerResponse searchProductList(String productName, Integer productId, Integer pageNum, Integer pageSize) {
        if(StringUtils.isNotBlank(productName)){
            productName = new StringBuilder().append("%").append(productName).append("%").toString();
        }

        PageHelper.startPage(pageNum,pageSize);
        List<Product> productList = productMapper.searchProductByNameOrId(productName,productId);
        List<ProductListVo> productListVoList = Lists.newArrayList();
        for(Product product:productList){
            productListVoList.add(assembleProductListVo(product));
        }
        PageInfo pageInfo = new PageInfo(productListVoList);

        return ServerResponse.createBySuccess(pageInfo);
    }
}
